package com.vcredit.creditcat.manager.zmxy;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.bird.service.zmxy.ZmxyProcessService;
import com.vcredit.bird.vo.zmxy.ZmxyCallBackResult;
import com.vcredit.bird.vo.zmxy.ZmxyQueryResult;
import com.vcredit.creditcat.api.IZmxyProcessManager;
import com.vcredit.creditcat.dto.zmxy.ZmxyCallBackDto;
import com.vcredit.creditcat.dto.zmxy.ZmxyQueryDto;
import com.vcredit.creditcat.enumtype.AuthItemEnum;
import com.vcredit.creditcat.enumtype.EMStatusEnum;
import com.vcredit.creditcat.exception.CreditZmxyException;
import com.vcredit.creditcat.manager.CreditCatProcessManager;
import com.vcredit.creditcat.model.AuthItem;
import com.vcredit.creditcat.model.AuthLog;
import com.vcredit.creditcat.model.AuthSecredit;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.service.AuthLogService;
import com.vcredit.creditcat.service.AuthSecreditService;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.util.Constants;
import com.vcredit.framework.annotation.Log;

@Service(protocol = { "protocol1" }, version = "1.0")
public class ZmxyProcessManager implements IZmxyProcessManager {

	@Autowired
	private ZmxyProcessService zmxyProcessService;

	@Autowired
	private AuthSecreditService AuthSecreditService;

	@Autowired
	private CreditCatProcessManager creditCatProcessManager;

	@Autowired
	private AuthLogService authLogService;

	@Override
	@Log
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public ZmxyQueryDto zmxyQuery(Cust cust) throws Exception {
		String zmxyScore = null;
		Integer zmxyStatus = 1;
		String bindUrl = null;
		String expiredTime = null;
		AuthSecredit authSecredit = AuthSecreditService.findByProcessId(cust.getLastProccessId());
		ZmxyQueryResult result = zmxyProcessService.zmxyQuery(cust.getCustIdno(), cust.getCustName(),
				Constants.ZMXY_BUSTYPE, Constants.ZMXY_PLATFORM);
		Integer zmxyCreditStatus = null;
		Integer zmxyStatusCode = result.getStatusCode();
		if (0 == zmxyStatusCode && StringUtils.isNotBlank(result.getResult())) {
			expiredTime = ConcurrentDateUtil.formatDateTime(ConcurrentDateUtil.addDayTime(new Date(), 30));
			creditCatProcessManager.itemDone(new AuthItem(AuthItemEnum.SESAME_CREDIT.getCode()),
					cust.getLastProccessId());
			authLogService.insert(new AuthLog(authSecredit.getProcessId(), cust.getCustId(),
					AuthItemEnum.SESAME_CREDIT.getCode(), expiredTime, new Date()));
			zmxyCreditStatus = EMStatusEnum.AUTH_SUCCESS.getCode();
			zmxyScore = result.getResult();
			zmxyStatus = 0;
		} else if (2 == zmxyStatusCode) {// 跳转授权
			bindUrl = result.getBindUrl();
		} else {
			throw new CreditZmxyException(result.getStatusDescription());
		}
		AuthSecreditService.update(new AuthSecredit(authSecredit.getZmxyProcessId(), zmxyScore, ConcurrentDateUtil.formatDateTime(new Date()),
				zmxyCreditStatus, zmxyStatusCode, expiredTime == null ? "" : expiredTime));
		return new ZmxyQueryDto(zmxyStatus, zmxyScore, getZmxyRealAuthTime(new Date()), bindUrl);
	}

	@Override
	@Log
	public ZmxyCallBackDto zmxyCallBack(String params, String sign) throws Exception {
		ZmxyCallBackResult result = zmxyProcessService.zmxyCallBack(params, sign);
		return new ZmxyCallBackDto(result.getOperationStatus(), result.getDesc());
	}

	/**
	 * 得到芝麻信用真正的授权时间
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	protected long getZmxyRealAuthTime(Date date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DATE);
		if (day < 6) {
			calendar.add(Calendar.MONTH, -1); // 得到前一个月
		}
		String ymd = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-06";
		return ConcurrentDateUtil.getDate(ymd).getTime();
	}
}
